/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-12-06 13:40:04
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:30:25
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent ,TreeNode} from 'primeng/api';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { BomContentServiceProxy, MaterialsServiceProxy ,InsertDetailsDto} from '@shared/service-proxies/service-proxies';


@Component({
    selector: 'app-choose-bom',
    templateUrl: './choose-bom.component.html',
    styleUrls: ['./choose-bom.component.less']
})
export class ChooseBomComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() select: any;  //行数据
    @Input() oldData: any;  //行数据
    saving = false;
    active = false;
    selectRow: any = [];
    treeData = [];
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _materialsServiceProxy: MaterialsServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
    }
    selectStatus(value) {
        switch (value) {
            case 0:
                return '编制中';
            case 1:
                return '审核中';
            case 2:
                return '被驳回';
            case 3:
                return '已发布';
            case 4:
                return '已维护';
            case 5:
                return '待维护';
            default:
                return null;
        }
    }
    //打开弹窗
    onShown(): void {
        this.getTreedata();
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.selectRow = [];
        this.treeData = [];
        this.active = false;
        this.modal.hide();
    }
    getTreedata() {
        abp.ui.setBusy();
        this._materialsServiceProxy.getBomMaterialsContentAll(
            this.select.modelId,
            this.select.lotNoId
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            abp.ui.clearBusy();
            this.treeData = result;
            this.treeData[0].data.levelNum = 1;
            this.expandAll();
        });
    }
    expandAll() {
        for (const node of this.treeData) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        }
    }
    save() {
        for (const item of this.selectRow) {
            for (const son of this.oldData) {
                if (item.data.materialCode == son.code) {
                    this._messageService.info(item.data.materialNo+'已存在请勿添加相同的物料');
                    return;
                }
            }
        }
        let data = [];
        for (const item of this.selectRow) {
            let rs = new InsertDetailsDto();
            rs.code = item.data.materialCode;
            rs.name = item.data.materialName;
            rs.number = item.data.unit1BomDosage;
            rs.remark = item.data.remark;
            data.push(rs)
        }
        this.modalSave.emit(data);
        this.close();
    }
}
